package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Mycollect;
import net.zjitc.entity.Mycompany;
import net.zjitc.entity.VO.RecruitVo;
import net.zjitc.service.IManagerService;
import net.zjitc.service.IMycollectService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @auther 郭梦婷
 * @create 2022-05-30 21:33:24
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "用户收藏的岗位")
    public class MycollectController {
    @Reference
    private IMycollectService mycollectService;
    ResponseResult result=new ResponseResult();


    @ApiOperation("收藏岗位")
    @GetMapping("AddCollect")
    public ResponseResult AddAttention(@RequestParam(required = false) Integer recruitid,
                                       @RequestParam(required = false) String username){
        if (username.length() != 0&&recruitid!=null){

            Mycollect mycollect=new Mycollect();
            mycollect.setRecruitid(recruitid);
            mycollect.setUsername(username);
            boolean save = mycollectService.save(mycollect);
            if (save){
                result.setSuccess("收藏岗位成功");
            }else {
                result.setError("收藏岗位失败");
            }
        }else {
            result.setError("没有数据");
        }
        return result;

    }
    @ApiOperation("是否收藏")
    @GetMapping("IfCollect")
    public ResponseResult IfCollect(@RequestParam(required = false) Integer recruitid,
                                    @RequestParam(required = false) String username){
        Mycollect mycollect= mycollectService.IfCollect(recruitid,username);
        if (mycollect!=null){
            result.setSuccess("已经收藏",mycollect);
        }else {
            result.setError("未收藏");
        }
        return result;
    }

    @ApiOperation("取消收藏")
    @GetMapping("DeleteCollect")
    public ResponseResult DeleteCollect(@RequestParam(required = false) Integer recruitid,
                                          @RequestParam(required = false) String username){
        int delete= mycollectService.DeleteCollect(recruitid,username);
        if (delete!=0){
            result.setSuccess("取消收藏成功");
        }else {
            result.setError("取消收藏失败");
        }
        return result;
    }
    @ApiOperation("我的全部收藏")
    @GetMapping("getMyCollectByUsername")
    public ResponseResult getMyCollectByUsername(@RequestParam(required = true) String username,
                                                 @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                                 @RequestParam(required = true,value = "pagesize") Integer pagesize) {
        Page<RecruitVo> recruit = mycollectService.getMyCollectByUsername(username,pagenum,pagesize);
        if (recruit.getTotal() != 0) {
            result.setSuccess("获取我的收藏成功",recruit);
        } else {
            result.setError("获取我的收藏失败");
        }
        return result;
    }


}

